package com.gp.shaw.netty.io.im;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gp.shaw.netty.io.im.model.IMMsg;
import com.gp.shaw.netty.io.im.model.IMOperator;

import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * Created by asus on 2018/11/3.
 */
public class IMClient {
    private String sendId;
    private String ip;
    private int port;
    private Socket socket;
    private ObjectOutputStream oos;
    private ObjectInputStream ois;
    public IMClient(String ip,int port,String sendId){
        this.ip = ip;
        this.port = port;
        this.sendId = sendId;
    }

    public void start(){
        try {
            socket = new Socket(ip, port);
            System.out.println("已启动");
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public void sendMsgToPerson(String targetId,String context){

        IMMsg msg = new IMMsg();
        msg.setOperator(IMOperator.SEND_PERSON);
        msg.setSender(sendId);
        msg.setContent(context);
        msg.setTarget(targetId);
        msg.setTime(System.currentTimeMillis());

        send(msg);
    }

    public void sendMsgToGroup(String targetId,String context){

        IMMsg msg = new IMMsg();
        msg.setOperator(IMOperator.SEND_GROUP);
        msg.setSender(sendId);
        msg.setContent(context);
        msg.setTarget(targetId);
        msg.setTime(System.currentTimeMillis());

        send(msg);
    }

    public void login(){
        System.out.println("登录");
        IMMsg msg = new IMMsg();
        msg.setOperator(IMOperator.LOGIN);
        msg.setSender(sendId);
        msg.setTime(System.currentTimeMillis());
        send(msg);
    }

    public void logout(){

        IMMsg msg = new IMMsg();
        msg.setOperator(IMOperator.LOGOUT);
        msg.setSender(sendId);
        msg.setTime(System.currentTimeMillis());


        send(msg);
    }

    public void addGroup(String groupId){

        IMMsg msg = new IMMsg();
        msg.setOperator(IMOperator.ADD_GROUP);
        msg.setSender(sendId);
        msg.setContent(groupId);
        msg.setTime(System.currentTimeMillis());


        send(msg);
    }

    public void send(IMMsg msg){
        try{
            oos = new ObjectOutputStream(socket.getOutputStream());
            oos.writeObject(msg);
            oos.flush();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void reciveMsg() throws IOException {
        try{
            while(true) {
                if(socket==null||socket.isClosed()){
                    start();
                }
                ois = new ObjectInputStream(socket.getInputStream());
                IMMsg msg = (IMMsg) ois.readObject();
                System.out.println(msg.getSender() + ":" + msg.getContent());
            }
        } catch (UnknownHostException e) {
            e.printStackTrace();
            close();
        } catch (IOException e) {
            e.printStackTrace();
            close();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            close();
        }
    }

    public void close() throws IOException {

        if(oos!=null){
            oos.close();
            oos=null;
        }
        if(ois!=null){
            ois.close();
            ois=null;
        }
        if(socket!=null){
            socket.close();
            socket=null;
        }
    }

}
